package com.zye.servlet;

import com.alibaba.fastjson2.JSON;
import com.zye.JDBCUtils;
import com.zye.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/idselect")
public class idselect extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        String id = req.getParameter("id");
        // 根据 ID 查询学生
        String sql = "SELECT * FROM student WHERE id = ?";
        List<Student> students = JDBCUtils.query(Student.class, sql, Integer.parseInt(id));
        // 转换为JSON格式
        String str = JSON.toJSONString(students.get(0));
        // 返回响应
        resp.getWriter().write(str);
    }
}
